﻿using Inovout.Cloud.DataWarehouse.Models;
using Inovout.Cloud.DataWarehouse.Services;
using Inovout.Cloud.DataWarehouse.Web.Common;
using Inovout.Cloud.DataWarehouse.Web.Utility;
using Inovout.Cloud.Web.OData;
using Microsoft.Data.Edm;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Query;

namespace Inovout.Cloud.DataWarehouse.Web.Controllers
{
    public class DataEntityController:ApiController
    {
        private IDataEntityService dataEntityService;
        private IDataCollectionService dataCollectionService;
        //private IDataSourceService dataSourceService;

        public DataEntityController()
        {
            this.dataEntityService = DataEntityService.GetInstance();
            this.dataCollectionService = ComponentRegistry.Resolve<IDataCollectionService>();
            //this.dataSourceService = ComponentRegistry.Resolve<IDataSourceService>();
        }

        [Route("v1/pushData")]
        public void POST(object obj)
        {
            if(obj==null){
                return;
            }

            JObject jObject = JObject.FromObject(obj);
            //bool isOk = checkData(jObject);//暂时不验证
            bool isOk = true;
            if(isOk){
                //发送数据到kafka
                HttpUtils.PutDataToKafka(jObject);
            }
        }

        private bool checkData(JObject　jObject) { 
            //TODO 验证数据是否正确
            string dataSource = jObject["dataSource"].ToString();
            string dataView = jObject["dataView"].ToString();
            //DataSource source = dataSourceService.FindByName(dataSource);
            //if (source.OriginalDataViews != null && source.OriginalDataViews.Count() > 0)
            //{
            //    Iesi.Collections.Generic.ISet<OriginalDataView> originalDataViews = source.OriginalDataViews;
            //    foreach (OriginalDataView originalDataView in originalDataViews)
            //    {
            //        if (originalDataView.FullName.Equals(dataView))
            //        {
            //            return true;
            //        }
            //    }
            //}
            return false;
        }

        //GET https://www.xx.com/dataWarehouse/v1/dataEntity/{dataCollectionName}?<Filter>
        [Route("v1/q/{dataCollectionName}")]
        public object Get(string dataCollectionName)
        {
            IQueryable query =  dataEntityService.QueryAll(dataCollectionName);

            return JsonUtility.ToJson(query);
        }

        //GET 根据条件查询数据
        [Route("v1/q/Query/{dataCollectionName}")]
        [HttpGet]
        [ActionName("Get")]
        public string Query(string dataCollectionName) 
        {
            IQueryable result = null;
            if (!CheckParameter(null)) 
            {
                return JsonUtility.ToJson(result);//参数校验不通过
            }

            IQueryable query = dataEntityService.QueryAll(dataCollectionName);
            if(query == null)
            {
                return JsonUtility.ToJson(result);
            }
            IEdmModel edmModel = ODataModelHelper.GetEdmModel(query);
            var options = new ODataQueryOptions(new ODataQueryContext(edmModel, query.ElementType), this.Request);
            IQueryable queryResult = options.ApplyTo(query);
            result = queryResult;
            return JsonUtility.ToJson(result);
        }
        private bool CheckParameter(ODataQueryOptions opts) 
        {
            //TODO
            return true;
        }
    }
}